package com.boyi.shared.kafka;

import com.boyi.shared.util.SerializeUtil;
import org.apache.kafka.common.errors.SerializationException;
import org.apache.kafka.common.serialization.Serializer;

import java.io.IOException;
import java.util.Map;

public class KafkaObjectSeralizer<T> implements Serializer<T> {

    @Override
    public void configure(Map<String, ?> configs, boolean isKey) {
    }

    @Override
    public byte[] serialize(String topic, T data) {
        try {
            if (data == null) {
                return null;
            } else {
                return SerializeUtil.serialize(data);
            }
        } catch (IOException e) {
            throw new SerializationException("Error when serializing " + data.getClass() + " to byte[] due to IOException ", e);
        }
    }

    @Override
    public void close() {
        // nothing to do
    }


}

